import { generateRandomId } from '@/lib/utils';
import { OperationMessage, useMessageStore } from '@/store/message';

export function useMessage() {
  const dispatch = useMessageStore((state) => state.dispatch);

  function resetMessage() {
    dispatch({
      type: 'update',
      update(draftState) {
        draftState.operationMessages = [];
      },
    });
  }

  function addMessage(message: Omit<OperationMessage, 'id'>) {
    // console.log(message.type, message.message);
    dispatch({
      type: 'update',
      update(draftState) {
        draftState.operationMessages.push({
          ...message,
          id: generateRandomId(),
        });
      },
    });
  }

  return {
    resetMessage: useCallback(resetMessage, []),
    addMessage: useCallback(addMessage, []),
  };
}
